/*
 * Copyright 2003,2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.jakubiak.javabean;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.LinkedList;

/**
 *
 * @author Antek
 */
public class BeanProperty implements AnnotatedElement {

    /**
     * Field is unused,
     * JavaBean prefers getter and setter
     */
    private Field field;
    /**
     * getter method
     */
    private Method getterMethod;
    /**
     * setter method
     */
    private Method setterMethod;
    /**
     * Type of property
     */
    @SuppressWarnings("unchecked")
	private Class type;
    
    /**
     * declaring class
     */
    @SuppressWarnings("unchecked")
	private Class myClass;

    /**
     * Conctructor
     * 
     * @param myClass
     * @param field
     * @param getterMethod
     * @param setterMethod
     */
    @SuppressWarnings("unchecked")
	public BeanProperty(Class myClass, Field field, Method getterMethod, Method setterMethod) {
        this.myClass = myClass;
        this.type = (Class) field.getType();
        this.field = field;
        this.getterMethod = getterMethod;
        this.setterMethod = setterMethod;
    }

    /**
     * Get getter method
     * @return
     */
    public Method getGetterMethod() {
        return getterMethod;
    }

    /**
     * Get setter method
     */
    public Method getSetterMethod() {
        return setterMethod;
    }

//    private boolean checkTypes() {
//        if (!type.equals(field.getType())) {
//            return false;
//        }
//        if (!field.getType().equals(getterMethod.getReturnType())) {
//            return false;
//        }
//        if (!field.getType().equals(setterMethod.getParameterTypes()[0])) {
//            return true;
//        }
//        return true;
//    }

    /**
     * Get type of propetry
     * @return
     */
    @SuppressWarnings("unchecked")
	public Class getType() {
        return type;
    }

    /**
     * Get a class with this property
     * @return
     */
    @SuppressWarnings("unchecked")
	public Class getOwnerClass() {
        return myClass;
    }
    
    

    /**
     * Get a parameterized type such as
     * Collection&lt;String&gt;.
     * 
     * @return parameterized type of field
     */
    public ParameterizedType getParameterizedType() {
        return (ParameterizedType)field.getGenericType();
    }
    
    
    /**
     * Get value of bean
     * @param bean
     * @return
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.IllegalArgumentException
     * @throws java.lang.reflect.InvocationTargetException
     */
    public Object get(Object bean)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        return getGetterMethod().invoke(bean);
    }

    /**
     * Set value of bean
     * @param bean
     * @return
     * @throws java.lang.IllegalAccessException
     * @throws java.lang.IllegalArgumentException
     * @throws java.lang.reflect.InvocationTargetException
     */
    public void set(Object bean, Object value)
            throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        getSetterMethod().invoke(bean, value);
    }

    /**
     * Name of java bean property, for example: "placeName"
     * @return
     */
    public String getName() {
        return field.getName();
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        if(field.isAnnotationPresent(annotationClass)) {
            return field.getAnnotation(annotationClass);
        }
        if(getterMethod.isAnnotationPresent(annotationClass)) {
            return getterMethod.getAnnotation(annotationClass);
        }
        if(setterMethod.isAnnotationPresent(annotationClass)) {
            return setterMethod.getAnnotation(annotationClass);
        }
        return null;
    }

    public Annotation[] getAnnotations() {
        Collection<Annotation> annotations = new LinkedList<Annotation>();
        for(Annotation annotation : field.getAnnotations()) {
            annotations.add(annotation);
        }
        for(Annotation annotation : getterMethod.getAnnotations()) {
            annotations.add(annotation);
        }
        for(Annotation annotation : setterMethod.getAnnotations()) {
            annotations.add(annotation);
        }
        return (Annotation[]) annotations.toArray();
    }

    public Annotation[] getDeclaredAnnotations() {
        Collection<Annotation> annotations = new LinkedList<Annotation>();
        for(Annotation annotation : field.getDeclaredAnnotations()) {
            annotations.add(annotation);
        }
        for(Annotation annotation : getterMethod.getDeclaredAnnotations()) {
            annotations.add(annotation);
        }
        for(Annotation annotation : setterMethod.getDeclaredAnnotations()) {
            annotations.add(annotation);
        }
        return (Annotation[]) annotations.toArray();
    }

    public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
        return field.isAnnotationPresent(annotationClass) 
                || getterMethod.isAnnotationPresent(annotationClass) 
                || setterMethod.isAnnotationPresent(annotationClass);
    }
    
    
    
    
    
    
}
